var dom = document.getElementById('main');
var myChart = echarts.init(dom);
var option;
let dataChange = false;
let data = [];
// Parametric curve
for (var t = 0; t < 25; t += 0.001) {
    var x = (1 + 0.25 * Math.cos(75 * t)) * Math.cos(t);
    var y = (1 + 0.25 * Math.cos(75 * t)) * Math.sin(t);
    var z = t + 2.0 * Math.sin(75 * t);
    data.push([x, y, z]);
}
var color = [
    '#313695',
    '#4575b4',
    '#74add1',
    '#abd9e9',
    '#e0f3f8',
    '#ffffbf',
    '#fee090',
    '#fdae61',
    '#f46d43',
    '#d73027',
    '#a50026'
];
initOption();// 有一个可以自定义的option参数
setData(data);// 二维数据
setColor(color);// 数组
setBackgroundColor("#f5f5f5")// 16进制颜色
draw();
window.addEventListener('resize', myChart.resize);

function initOption(redefine=null){
    if(redefine!=null){
        option = redefine;
        return;
    }
    option = {
        tooltip: {},
        backgroundColor: '#fff',
        visualMap: {
            show: false,
            dimension: 2,
            min: 0,
            max: 30,
            inRange: {
                color: []
            }
        },
        xAxis3D: {
            type: 'value'
        },
        yAxis3D: {
            type: 'value'
        },
        zAxis3D: {
            type: 'value'
        },
        grid3D: {
            viewControl: {
                projection: 'orthographic'
            }
        },
        series: [
            {
                type: 'line3D',
                data: [],
                lineStyle: {
                    width: 4
                }
            }
        ]
    };
}
function setData(dataL,num=0){
    if(option?.series[num]?.data){
        data = dataL;
        option.series[num].data = dataL;
        dataChange = true;
        draw();
    }
}
function getData(){
    return data;
}
function setColor(color,min=0,max=30){
    if(option?.visualMap){
        var visualMap = option.visualMap;
        if(visualMap?.inRange?.color){
            visualMap.inRange.color = color;
            visualMap.min = min;
            visualMap.max = max;
        }
        draw();
    }
}
function setBackgroundColor(color){
    if(option?.backgroundColor){
        option.backgroundColor = color;
    }
}
function draw(){
    if (option && typeof option === 'object') {
        myChart.setOption(option);
    }
}
